package component.controls
{
	import component.controls.ui.Component;
	import component.global.ComponentSetting;
	import component.global.manager.EventMgr;
	import component.global.manager.StageManager;
	import component.global.utils.DisplayUtils;
	import component.global.utils.GraphicsUtils;
	import component.global.utils.ObjectUtil;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class TextInput extends Component
	{
		public function TextInput($width:int = ComponentSetting.TEXTINPUT_DEFAULT_WIDTH, $height:int = ComponentSetting.TEXTINPUT_DEFAULT_HEIGHT)
		{
			_width = $width;
			_height = $height;
			super();
		}
		
		protected var _textField:TextField;
		
		override protected function init():void{
			super.init();
			_textField = new TextField();
			_background = GraphicsUtils.createRectangle(_width,_height);
			_background.filters = _filters[0];
			_textField.width = _width - 4;
			_textField.height = _height - 2;
			_textField.defaultTextFormat = new TextFormat("songti",12);
			_textField.x = _textField.y = 2;
			_textField.type = "input";
			
			
			EventMgr.Instance.addEvent(_textField, [MouseEvent.ROLL_OVER, MouseEvent.ROLL_OUT, FocusEvent.FOCUS_IN,FocusEvent.FOCUS_OUT],[__onEventHandler,__onEventHandler,__onEventHandler,__onEventHandler]);
			DisplayUtils.addDisplayObject(this, _background, _textField);
		}
		
		public function set text(value:String):void{
			_textField.text = value;
		}
		
		public function get text():String{
			return _textField.text;
		}
		
		public function get maxChars():int{
			return _textField.maxChars;
		}
		
		public function set maxChars(value:int):void{
			_textField.maxChars = value;
		}
		
		protected function __onEventHandler(e:Event):void{
			switch(e.type){
				case FocusEvent.FOCUS_IN:
					_background.filters = _filters[2];
					break;
				case FocusEvent.FOCUS_OUT:
					_background.filters = _filters[0];
					break;
				case MouseEvent.ROLL_OVER:
					if(StageManager.Instance.stage.focus == _textField)return;
					_background.filters = _filters[1];
					break;
				case MouseEvent.ROLL_OUT:
					if(StageManager.Instance.stage.focus == _textField)return;
					_background.filters = _filters[0];
					break;
				default:
					_background.filters = _filters[0];
					break;
			}
		}
		
		override public function dispose():void{
			super.dispose();
			ObjectUtil.dispose(_textField);_textField=null;
		}
	}
}